/*
 * Copyright (C) 2019 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.yoyo.fragment.communication;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.xuexiang.yoyo.R;
import com.xuexiang.yoyo.core.BaseFragment;
import com.xuexiang.yoyo.databinding.FragmentCommunicationBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.DensityUtils;
import com.xuexiang.xui.utils.XToastUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xui.widget.grouplist.XUICommonListItemView;
import com.xuexiang.xui.widget.grouplist.XUIGroupListView;

/**
 * @author liuxiangfei
 * @since 2023-07-24 00:19
 */
@Page(anim = CoreAnim.none)
public class CommunicationFragment extends BaseFragment<FragmentCommunicationBinding> {
    XUIGroupListView mGroupListView;
    @NonNull
    @Override
    protected FragmentCommunicationBinding viewBindingInflate(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot) {
        return FragmentCommunicationBinding.inflate(inflater, container, attachToRoot);
    }

    /**
     * @return 返回为 null意为不需要导航栏
     */
    @Override
    protected TitleBar initTitle() {
        return super.initTitle().setTitle("通讯");
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initViews() {
        mGroupListView = findViewById(R.id.groupListView);
        initGroupListView();
    }

    private void initGroupListView() {

        XUICommonListItemView itemWithSwitch1 = mGroupListView.createItemView("蓝牙");
        itemWithSwitch1.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        itemWithSwitch1.getSwitch().setOnCheckedChangeListener((buttonView, isChecked) -> XToastUtils.toast("checked = " + isChecked));



        XUICommonListItemView itemWithSwitch2 = mGroupListView.createItemView("红外");
        itemWithSwitch2.setAccessoryType(XUICommonListItemView.ACCESSORY_TYPE_SWITCH);
        itemWithSwitch2.getSwitch().setOnCheckedChangeListener((buttonView, isChecked) -> XToastUtils.toast("checked = " + isChecked));




         View.OnClickListener onClickListener = v -> {
            if (v instanceof XUICommonListItemView) {
                XUICommonListItemView xv = (XUICommonListItemView) v;
                CharSequence text = xv.getText();
                //XToastUtils.toast(text + " is Clicked  ");
            }
        };

        int size = DensityUtils.dp2px(getContext(), 20);
        XUIGroupListView.newSection(getContext())
                .setTitle("通讯方式")
                .setLeftIconSize(size, ViewGroup.LayoutParams.WRAP_CONTENT)
                .addItemView(itemWithSwitch1, onClickListener)
                .addItemView(itemWithSwitch2, onClickListener)
                .addTo(mGroupListView);


    }

}
